/*
 * ------------------------------------------------------------------
 * Copyright © 2017 Hangzhou DtDream Technology Co.,Lt d. All rights reserved.
 * ------------------------------------------------------------------
 * Product: DataStudio
 * Module Name:
 * Date Created: 2017-10-23
 * Description:
 * ------------------------------------------------------------------
 * Modification History
 * DATE Name Description
 * ------------------------------------------------------------------
 * 2017-10-23 八神 h0248
 * ------------------------------------------------------------------
 */

package org.xukai.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JacksonUtil {

    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    /**
     * Json格式的字符串向JavaBean转换，传入空串将返回null
     * @param strBody Json格式的字符串
     * @param c 目标JavaBean类型
     * @return JavaBean对象, 如果解析失败返回 null
     */
    public static <T> T parseObject(String strBody, Class<T> c) {
        if (strBody == null || "".equals(strBody)) {
            return null;
        } else {
            try {
                return OBJECT_MAPPER.readValue(strBody, c);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     *
     * @param strBody
     * @return
     */
    public static JsonNode parseJsonNode(String strBody) {
        if (strBody == null || "".equals(strBody)) {
            return null;
        } else {
            try {
                return OBJECT_MAPPER.readTree(strBody);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * Json格式的字符串向JavaBean List集合转换，传入空串将返回空集合
     * @param strBody
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> parseArray(String strBody, Class<T> c) {
        if (strBody == null || "".equals(strBody)) {
            return new ArrayList<>(0);
        } else {
            JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c);
            try {
                return (List<T>) OBJECT_MAPPER.readValue(strBody, javaType);
            } catch (IOException e) {
                e.printStackTrace();
                return new ArrayList<>(0);
            }
        }
    }

    /**
     * Json格式的字符串向List<String>集合转换，传入空串将返回null
     * @param strBody
     * @return
     * @throws IOException
     */
    public static List<String> json2List(String strBody){
        return parseArray(strBody, String.class);
    }

    /**
     * Object转为Json格式字符串的方法
     * @param o
     * @return
     */
    public static String toJsonString(Object o){
        try {
            return OBJECT_MAPPER.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

}
